﻿namespace MapleRock.P2PEmulator.Core
{
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Xml;
  using System.Xml.Serialization;

  [Serializable]
  [XmlRoot("Report")]
  public sealed class XmlReport
  {
    public XmlReport()
    {
      this.InputData = new XmlInputData();
      this.InitialState = new List<XmlPeer>();
      this.Cities = new List<XmlCity>();
    }

    [XmlElement(IsNullable = false)]
    public XmlInputData InputData { get; set; }

    [XmlArray(IsNullable = false, ElementName = "InitialState")]
    [XmlArrayItem(ElementName = "Peer", IsNullable = false)]
    public List<XmlPeer> InitialState { get; set; }

    [XmlArray(IsNullable = false, ElementName = "Cities")]
    [XmlArrayItem(ElementName = "City", IsNullable = false)]
    public List<XmlCity> Cities { get; set; }

    [XmlAttribute("citiesAdjacency")]
    public string CitiesAdjacency { get; set; }

    [XmlAttribute("runtimeSeconds")]
    public int RuntimeSeconds { get; set; }

    public static XmlReport Load(string path)
    {
      var serializer = new XmlSerializer(typeof(XmlReport));
      var report = serializer.Deserialize(XmlReader.Create(path));
      return (XmlReport)report;
    }

    public void Save(string path)
    {
      var settings = new XmlWriterSettings();
      settings.Indent = true;
      settings.IndentChars = "  ";

      var writer = XmlTextWriter.Create(path, settings);

      var serializer = new XmlSerializer(typeof(XmlReport));
      serializer.Serialize(writer, this);

      writer.Close();
    }
  }
}
